﻿using CarAppClient.Consts;
using CarAppClient.Controls.MessageUsercontrols;
using CarAppClient.Extension;
using CarAppClient.Helpers;
using CarAppClient.ViewModels;
using CarAppClient.Views.CarParts;
using CarAppClient.Views.CarType.Views;
using CarAppDto.CarParts.CarPartsDtos;
using CarAppDto.CarParts.CarPartsTypeDtos;
using CarAppDto.CarType.AreaDtos;
using CarAppDto.CarType.CarDtos;
using CarAppDto.CarType.VehicleSeriesDtos;
using CarAppDto.Common;
using D.UtilCore.Dtos;

namespace CarAppClient.Views.CarType.ViewModels.CarType
{
    public class CarPartsModifyViewModel : BaseModifyViewModel<Page_CarParts_Modify>
    {
        #region 加载页面数据
        string _vehicleSeriesId = null;
        public CarPartsModifyViewModel(Page_CarParts_Modify owner, string? id, string vehicleSeriesId, ViewStatus? viewStatus) : base(owner, id, viewStatus)
        {
            _vehicleSeriesId = vehicleSeriesId;
            LoadData();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="repCarType"></param>
        protected async override void LoadData()
        {
            if (!string.IsNullOrEmpty(Id))
            {
                // 请求后端接口 
                var res = await HttpHelper<RepCarParts>.GetAsync($"{ApiUrl.GetCarParts}/{Id}");

                //  判断结果
                if (res.Code == StatusCodeEnum.Waring)
                {
                    MsgUtil.Error(res.ErrorMsg);
                }
                else if (res.Code == StatusCodeEnum.Error)
                {
                    MsgUtil.Error("服务器异常");
                }
                else
                {
                    // 绑定数据源到UI 
                    if (res.Data != null)
                    {
                        var resCarPartsType = await HttpHelper<RepVehicleSeriesCarPartsType>.GetAsync($"{ApiUrl.GetVehicleSeriesCarPartsType}/{res.Data.CarPartsTypeId}");
                        repCarPartsType = resCarPartsType.Data;

                        Name = res.Data.Name;
                        Code = res.Data.Code;
                        EnglishName = res.Data.EnglishName;
                        CarPartsTypeName = repCarPartsType?.Name ?? string.Empty;

                        _vehicleSeriesId = res.Data.VehicleSeriesId;
                    }
                }
            }

            var resVehicleSeries = await HttpHelper<RepVehicleSeries>.GetAsync($"{ApiUrl.GetVehicleSeries}/{_vehicleSeriesId}");
            repVehicleSeries = resVehicleSeries.Data;
        }
        #endregion

        #region 新增、修改 参数

        private string? name;
        /// <summary>
        /// 名称
        /// </summary>
        public string? Name
        {
            get { return name; }
            set { name = value; RaisePropertyChanged(); }
        }

        private string? code;
        /// <summary>
        /// 编号
        /// </summary>
        public string? Code
        {
            get { return code; }
            set { code = value; RaisePropertyChanged(); }
        }

        private string? englishName;
        /// <summary>
        /// 描述
        /// </summary>
        public string? EnglishName
        {
            get { return englishName; }
            set { englishName = value; RaisePropertyChanged(); }
        }

        RepVehicleSeriesCarPartsType repCarPartsType = null;
        /// <summary>
        /// 配件类型
        /// </summary>
        private string? carPartsTypeName;
        public string? CarPartsTypeName
        {
            get { return carPartsTypeName; }
            set { carPartsTypeName = value; RaisePropertyChanged(); }
        }

        /// <summary>
        /// 车系
        /// </summary>
        RepVehicleSeries repVehicleSeries = null;
        #endregion

        #region 保存事件
        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="vm"></param>
        protected override async void Submit(object? obj)
        {
            // 基础数据判断
            if (string.IsNullOrEmpty(Name))
            {
                MsgUtil.Warning("区域名称不可为空"); return;
            }

            // 基础数据判断
            if (string.IsNullOrEmpty(Id))
            {
                // 请求后端接口 
                var res = await HttpHelper<int>.PostAsync(ApiUrl.AddCarParts, new ReqAddCarParts()
                {
                    CarPartsTypeId = repCarPartsType?.Id,
                    Name = Name,
                    Code = Code,
                    EnglishName = EnglishName,
                    BrandId = repVehicleSeries?.BrandId,
                    VehicleSeriesId = repVehicleSeries?.Id
                }); ;

                //  判断结果
                MsgUtil.ShowMsg(res.Code, "新增成功", res.ErrorMsg);
            }
            else
            {
                // 请求后端接口 
                var res = await HttpHelper<int>.PutAsync($"{ApiUrl.UpdateCarParts}", new ReqUpdateCarParts()
                {
                    CarPartsTypeId = repCarPartsType?.Id,
                    Id = Id,
                    Name = Name,
                    Code = Code,
                    EnglishName = EnglishName,
                    BrandId = repVehicleSeries?.BrandId,
                    VehicleSeriesId = repVehicleSeries?.Id
                });
                // 判断结果
                MsgUtil.ShowMsg(res.Code, "修改成功", res.ErrorMsg);
            }
            this.FromClose();
        }
        #endregion

        #region 配件类型
        protected override async void Query(string? type)
        {
            Page_VehicleSeriesCarPartsType_Query area_Query = new Page_VehicleSeriesCarPartsType_Query(vehicleSeriesId:_vehicleSeriesId);
            ((VehicleSeriesCarPartsTypeQueryViewModel)area_Query.DataContext).InformationRequested += ChildViewModel_InformationRequested;
            area_Query.ShowDialog();
        }

        private void ChildViewModel_InformationRequested(object sender, InformationEventArgs<RepVehicleSeriesCarPartsType> e)
        {
            // 处理从ChildViewModel传递过来的信息
            repCarPartsType = e.Information;
            CarPartsTypeName = repCarPartsType?.Name;
        }
        #endregion 
    }
}
